package com.bussiness.system.service;

import com.common.vo.MenuVo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取权限树结构
 */
@Component
public class MenuVoUtil {

    public MenuVo getListMenuVo(List<MenuVo> listMenu, String parentId){
        MenuVo menuVo = null;
        int length = listMenu.size()-1;
        for(int i=length;i>=0;i--){
            if(listMenu.size()<=i){
                break;
            }
            MenuVo menuVo1 = listMenu.get(i);
            if(parentId.equals(menuVo1.getId())){
                menuVo = menuVo1;
                listMenu.remove(i);
                putMenuVo(listMenu, menuVo1, menuVo.getId());
            }

        }


        return menuVo;
    }


    public void putMenuVo(List<MenuVo> listMenu,MenuVo menuVo,String parentId){
        List<MenuVo> listMenus = new ArrayList<>();
        for(int i=listMenu.size()-1;i>=0;i--){
            if(listMenu.size()==0){
                break;
            }
            MenuVo menuVo1 = listMenu.get(i);
            if(menuVo1.getParentId().equals(parentId)){
                listMenus.add(menuVo1);
                listMenu.remove(i);
                putMenuVo(listMenu, menuVo1, menuVo1.getId());
            }
            menuVo.setMenus(listMenus);
        }

    }
}